//
//  ConOldActivityDetailController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/7/24.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConOldActivityDetailController.h"
#import "OldActivityHeadCell.h"
#import "ClubDetailViewCell.h"
#import "ClubNetwork.h"
#import "MBProgressHUD+MJ.h"
#import <SDWebImageManager.h>
#import "ConClubShowViewController.h"
#import "ConPublicOldViewController.h"
#import "ClubShowImageTextCell.h"
#import "SRVideoPlayer.h"
#import "ShareContentModel.h"
#import "ShareTool.h"
#import "ConNewClubDetailVc.h"

@interface ConOldActivityDetailController ()

@property (nonatomic, strong) OldActivityDetailModel *model;
@property (nonatomic, strong) SRVideoPlayer *videoPlayer;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong)  UIView *layerView;
@property (nonatomic, strong) UIImageView *playerView;

@end

@implementation ConOldActivityDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavi];
    if (self.videoURL.absoluteString.length>0) {
        [self configHeaderView];
    }
    [self setUpTableView];
    [self getOldActivityDetail];
    
}
- (void)configHeaderView {
    _bottomView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, ScreenW, ScreenH/3))];
    _playerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH/3)];
    _playerView.backgroundColor = [UIColor blackColor];
    [_bottomView addSubview:_playerView];
    _videoPlayer = [SRVideoPlayer playerWithVideoURL:_videoURL playerView:_playerView playerSuperView:_playerView.superview withImageUrl:self.avatarUrl];
    _videoPlayer.playerEndAction = SRVideoPlayerEndActionStop;
    //[_videoPlayer play];
    _layerView = [[UIView alloc] initWithFrame:CGRectMake(ScreenW/2-30, ScreenH/3*0.5-30, 60, 60)];
    _layerView.backgroundColor = RGBColorOne(250);
    _layerView.alpha = 0.5;
    _layerView.layer.cornerRadius = 30;
    _layerView.clipsToBounds = YES;
    [_bottomView addSubview:_layerView];
    _button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _button.frame = CGRectMake(ScreenW/2-30, ScreenH/3*0.5-30, 60, 60);
    _button.layer.cornerRadius = 20.0;
    _button.backgroundColor = [UIColor clearColor];
    [_button setImage:[UIImage imageNamed:@"ic_video_player"] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(click) forControlEvents:(UIControlEventTouchUpInside)];
    [_bottomView addSubview:_button];
}
- (void)click{
    _button.hidden = YES;
    _layerView.hidden = YES;
    [_playerView setImage:nil];
    [_videoPlayer play];
}
- (void)setNavi
{
    UIButton *editBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [editBtn setImage:[UIImage imageNamed:@"活动分享"] forState:(UIControlStateNormal)];
    [editBtn addTarget:self action:@selector(editBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:editBtn];
}
- (void)editBtnClick
{
    ShareContentModel  *model = [[ShareContentModel alloc] init];
    model.title = self.model.clubName;
    model.sharePicUrl = self.model.avatarUrl;
    model.content =self.model.title;
    model.pageUrl = self.model.webDynamicUrl;
    NSLog(@"model.title = %@  model.sharePicUrl = %@model.content = %@ model.pageUrl = %@",model.title,model.sharePicUrl,model.content,model.pageUrl);
    [ShareTool share:model];
}
- (void)setUpTableView
{
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([OldActivityHeadCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([OldActivityHeadCell class])];
    [self.tableView registerClass:[ClubShowImageTextCell class] forCellReuseIdentifier:NSStringFromClass([ClubShowImageTextCell class])];
    self.tableView.tableHeaderView = _bottomView;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1+self.model.detailList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        CGFloat titleH = [self.model.title boundingRectWithSize:CGSizeMake(ScreenW-30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:Font(15)} context:nil].size.height;
        return 84+titleH;
    }
    ClubShowImageTextCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ClubShowImageTextCell class])];
    OldImageDetail *detailModel = self.model.detailList[indexPath.row-1];
    CGFloat cellH = [cell calculateOldDetailCellHWithModel:detailModel];
    detailModel.cellH = cellH;
    return cellH;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        OldActivityHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OldActivityHeadCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.model;
        cell.clubInBlock = ^(OldActivityDetailModel *model) {
            ConNewClubDetailVc *detailVc = [[ConNewClubDetailVc alloc]init];
            detailVc.ID = self.model.clubId;
            [self.navigationController pushViewController:detailVc animated:YES];
        };
        return cell;
    }
    ClubShowImageTextCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ClubShowImageTextCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.oldModel = self.model.detailList[indexPath.row-1];
    return cell;
}
- (void)getOldActivityDetail
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    if (self.isFromClub) {
        parame[@"activityId"] = @(self.ID);
    }else {
        parame[@"id"] = @(self.ID);
    }
    [self showClearBgWaiter];
    __WEAK
    [ClubNetwork oldActivityDetailWithParame:parame response:^(BOOL success, NSString *msg,OldActivityDetailModel *model) {
        if (success) {
            self.model = model;
            self.title = model.title;
            if (model.status != 1) {
                [self setNavi];
            }
            [self calculateImagesHeight:model.detailList];
        }else {
            [MBProgressHUD showError:msg];
            [weakSelf hideWaiter];
        }
    }];
}
//缓存图片计算图片高度
- (void)calculateImagesHeight:(NSArray *)arr
{
    __WEAK
    dispatch_group_t group = dispatch_group_create();
    for (OldImageDetail *model in arr) {
        dispatch_group_enter(group);
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:model.dynamicImage] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            dispatch_group_leave(group);
        }];
    }
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [weakSelf hideWaiter];//下载完图片在隐藏指示器
        [weakSelf.tableView reloadData];
    });
}

- (void)viewDidDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [_videoPlayer destroyPlayer];
}

@end
